package com.wlc.controller;

import com.wlc.pojo.AttendanceStats;
import com.wlc.pojo.Result;
import com.wlc.service.AttendanceStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stats/attendance")
@Slf4j
public class AttendanceStatsController {

    @Autowired
    private AttendanceStatsService attendanceStatsService;

    @GetMapping("/dept")
    public Result<List<AttendanceStats>> getDeptStats(@RequestParam(required = false) String startDate,
                                                      @RequestParam(required = false) String endDate) {
        log.info("获取部门考勤统计: startDate={}, endDate={}", startDate, endDate);
        try {
            List<AttendanceStats> stats = attendanceStatsService.getDeptAttendanceStats(startDate, endDate);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取部门考勤统计失败: {}", e.getMessage());
            return Result.error("获取部门考勤统计失败");
        }
    }

    @GetMapping("/personal/{employeeId}")
    public Result<List<AttendanceStats>> getPersonalDetail(@PathVariable Long employeeId,
                                                           @RequestParam(required = false) String startDate,
                                                           @RequestParam(required = false) String endDate) {
        log.info("获取个人考勤明细: employeeId={}, startDate={}, endDate={}", employeeId, startDate, endDate);
        try {
            List<AttendanceStats> detail = attendanceStatsService.getPersonalAttendanceDetail(employeeId, startDate, endDate);
            return Result.success(detail);
        } catch (Exception e) {
            log.error("获取个人考勤明细失败: {}", e.getMessage());
            return Result.error("获取个人考勤明细失败");
        }
    }

    @GetMapping("/daily")
    public Result<List<AttendanceStats>> getDailyStats(@RequestParam(required = false) String startDate,
                                                       @RequestParam(required = false) String endDate) {
        log.info("获取每日考勤统计: startDate={}, endDate={}", startDate, endDate);
        try {
            List<AttendanceStats> stats = attendanceStatsService.getDailyAttendanceStats(startDate, endDate);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取每日考勤统计失败: {}", e.getMessage());
            return Result.error("获取每日考勤统计失败");
        }
    }

    @GetMapping("/monthly")
    public Result<List<AttendanceStats>> getMonthlySummary(@RequestParam(required = false) String year) {
        log.info("获取月度考勤汇总: year={}", year);
        try {
            List<AttendanceStats> summary = attendanceStatsService.getMonthlyAttendanceSummary(year);
            return Result.success(summary);
        } catch (Exception e) {
            log.error("获取月度考勤汇总失败: {}", e.getMessage());
            return Result.error("获取月度考勤汇总失败");
        }
    }

    @GetMapping("/overview")
    public Result<Object> getOverview(@RequestParam(required = false) String startDate,
                                      @RequestParam(required = false) String endDate) {
        log.info("获取考勤统计总览: startDate={}, endDate={}", startDate, endDate);
        try {
            Object overview = attendanceStatsService.getAttendanceOverview(startDate, endDate);
            return Result.success(overview);
        } catch (Exception e) {
            log.error("获取考勤统计总览失败: {}", e.getMessage());
            return Result.error("获取考勤统计总览失败");
        }
    }
}